package com.example.yuejian.Activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yuejian.Bean.LoginBean;
import com.example.yuejian.Model.LoginModel;
import com.example.yuejian.R;
import com.example.yuejian.RetrofitListener;

public class LoginActivity extends Activity  implements RetrofitListener<LoginBean> {
    private EditText userView,passwordView;
    private TextView signView,forgetView;
    private Button loginbutton;
    private Intent intent;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        inintView();

    }

    public void inintView()
    {
        userView=(EditText) findViewById(R.id.username) ;
        passwordView=(EditText) findViewById(R.id.password);
        loginbutton=(Button) findViewById(R.id.loginbutton);
        signView=(TextView) findViewById(R.id.signView);
        forgetView=(TextView) findViewById(R.id.forgerView);
        sp=getSharedPreferences("me",MODE_PRIVATE);
        editor=sp.edit();
        loginbutton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                resultlogin();
            }
        });
        signView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                   intent=new Intent();
                   intent.setClass(LoginActivity.this,RegActivity.class);
                startActivityForResult(intent,1);

            }
        });


    }

  public void resultlogin(){
      LoginModel loginModel= new LoginModel();
      loginModel.userLogin(userView.getText().toString(),passwordView.getText().toString(),this);

  }




    @Override
    public void onSuccess(LoginBean error , int flag) {

        if("1".equals(error.getError()))
        {
            Toast.makeText(LoginActivity.this,error.getMsg(),Toast.LENGTH_SHORT).show();
        }else if ("0".equals(error.getError()))
        {
            SharedPreferences.Editor editor=sp.edit();
            editor.putString("username",userView.getText().toString());
            editor.commit();
            intent = new Intent(LoginActivity.this,MainActivity.class);
            startActivity(intent);

            Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();

        }else if("2".equals(error.getError()))
        {

            Toast.makeText(LoginActivity.this,error.getMsg(),Toast.LENGTH_SHORT).show();
        }
    }
  @Override
    public void onFail() {
 }



}
